package com.itkaien.demoCustomerRegisterBean;

import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;

import static com.itkaien.demoCustomerRegisterBean.BaseClassScanUtils.scanMyComponentAnnotation;

public class MyComponentBeanFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        Map<String, Class> stringClassMap = scanMyComponentAnnotation("com.itkaien");
        // 注册bean
        stringClassMap.forEach(
                (name, clazz) -> {

                    String clazzName = clazz.getName();
                    RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
                    rootBeanDefinition.setBeanClassName(clazzName);
                    registry.registerBeanDefinition(name, rootBeanDefinition);
                }
        );

    }
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        BeanDefinitionRegistryPostProcessor.super.postProcessBeanFactory(beanFactory);
    }
}
